package com.yzh.pinboard.CustomView;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.support.v4.content.res.ResourcesCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.yzh.pinboard.R;


public class PaintSettingView extends LinearLayout implements ColorPickerView.ColorListener,
        CompoundButton.OnCheckedChangeListener {

    private CheckBox mColor1st;
    private CheckBox mColor2nd;
    private CheckBox mColor3rd;
    private CheckBox mColor4th;
    private CheckBox mColor5th;
    private CheckBox mColor6th;
    private CheckBox mColor7th;
    private CheckBox mColor8th;
    private CheckBox mColor9th;
    private CheckBox mColor10th;

    public void setSmallWidth(float smallWidth) {
        this.smallWidth = smallWidth;
    }

    public void setNormalWidth(float normalWidth) {
        this.normalWidth = normalWidth;
    }

    public void setLargeWidth(float largeWidth) {
        this.largeWidth = largeWidth;
    }

    private float smallWidth = 1.0f;
    private float normalWidth = 5.0f;
    private float largeWidth = 20.0f;

    public PaintSettingView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_paint_setting, this);
        ColorPickerView colorPickerView = (ColorPickerView) view.findViewById(R.id.color_picker);
        RadioButton paintSmall = (RadioButton) view.findViewById(R.id.rBtn_paint_small);
        RadioButton paintNormal = (RadioButton) view.findViewById(R.id.rBtn_paint_normal);
        RadioButton paintLarge = (RadioButton) view.findViewById(R.id.rBtn_paint_large);
        paintSmall.setOnCheckedChangeListener(this);
        paintNormal.setOnCheckedChangeListener(this);
        paintLarge.setOnCheckedChangeListener(this);
        mColor1st = (CheckBox) view.findViewById(R.id.color_1st);
        mColor2nd = (CheckBox) view.findViewById(R.id.color_2nd);
        mColor3rd = (CheckBox) view.findViewById(R.id.color_3rd);
        mColor4th = (CheckBox) view.findViewById(R.id.color_4th);
        mColor5th = (CheckBox) view.findViewById(R.id.color_5th);
        mColor6th = (CheckBox) view.findViewById(R.id.color_6th);
        mColor7th = (CheckBox) view.findViewById(R.id.color_7th);
        mColor8th = (CheckBox) view.findViewById(R.id.color_8th);
        mColor9th = (CheckBox) view.findViewById(R.id.color_9th);
        mColor10th = (CheckBox) view.findViewById(R.id.color_10th);
        mColor1st.setOnCheckedChangeListener(this);
        mColor2nd.setOnCheckedChangeListener(this);
        mColor3rd.setOnCheckedChangeListener(this);
        mColor4th.setOnCheckedChangeListener(this);
        mColor5th.setOnCheckedChangeListener(this);
        mColor6th.setOnCheckedChangeListener(this);
        mColor7th.setOnCheckedChangeListener(this);
        mColor8th.setOnCheckedChangeListener(this);
        mColor9th.setOnCheckedChangeListener(this);
        mColor10th.setOnCheckedChangeListener(this);

        GradientDrawable drawable = (GradientDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.paint_black, null);
        drawable.setColor(Color.WHITE);
        mColor2nd.setBackground(drawable);

        //GradientDrawable drawable =(GradientDrawable)mColor2nd.getBackground();
        //drawable.setColor(Color.WHITE);

        colorPickerView.setColorListener(this);
    }

    public PaintSettingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    @Override
    public void onColorSelected(int color) {
        if (mPaintSettingListener != null) {
            mPaintSettingListener.selectColor(color);
        }
    }

    public void setPaintSettingListener(PaintSettingListener paintSettingListener) {
        mPaintSettingListener = paintSettingListener;
    }

    private PaintSettingListener mPaintSettingListener;

    private void setCheckableFalseExcept(CheckBox checkBox) {
        mColor1st.setChecked(false);
        mColor2nd.setChecked(false);
        mColor3rd.setChecked(false);
        mColor4th.setChecked(false);
        mColor5th.setChecked(false);
        mColor6th.setChecked(false);
        mColor7th.setChecked(false);
        mColor8th.setChecked(false);
        mColor9th.setChecked(false);
        mColor10th.setChecked(false);
        checkBox.setChecked(true);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (mPaintSettingListener == null) return;
        int i = buttonView.getId();
        if (i == R.id.color_1st) {
            if (isChecked) {
                setCheckableFalseExcept(mColor1st);
            }
            mPaintSettingListener.selectColor(R.color.color1st);
        } else if (i == R.id.color_2nd) {
            if (isChecked) {
                setCheckableFalseExcept(mColor2nd);
                mPaintSettingListener.selectColor(R.color.color2nd);
            }

        } else if (i == R.id.color_3rd) {
            if (isChecked) {
                setCheckableFalseExcept(mColor3rd);
                mPaintSettingListener.selectColor(R.color.color3rd);
            }

        } else if (i == R.id.color_4th) {
            if (isChecked) {
                setCheckableFalseExcept(mColor4th);
                mPaintSettingListener.selectColor(R.color.color4th);
            }

        } else if (i == R.id.color_5th) {
            if (isChecked) {
                setCheckableFalseExcept(mColor5th);
                mPaintSettingListener.selectColor(R.color.color5th);
            }

        } else if (i == R.id.color_6th) {
            if (isChecked) {
                setCheckableFalseExcept(mColor6th);
                mPaintSettingListener.selectColor(R.color.color6th);
            }
        } else if (i == R.id.color_7th) {
            if (isChecked) {
                setCheckableFalseExcept(mColor7th);
                mPaintSettingListener.selectColor(R.color.color7th);
            }
        } else if (i == R.id.color_8th) {
            if (isChecked) {
                setCheckableFalseExcept(mColor8th);
                mPaintSettingListener.selectColor(R.color.color8th);
            }
        } else if (i == R.id.color_9th) {
            if (isChecked) {
                setCheckableFalseExcept(mColor9th);
                mPaintSettingListener.selectColor(R.color.color9th);
            }
        } else if (i == R.id.color_10th) {
            if (isChecked) {
                setCheckableFalseExcept(mColor10th);
                mPaintSettingListener.selectColor(R.color.color10th);
            }
        } else if (i == R.id.rBtn_paint_small) {

            mPaintSettingListener.selectLineWidth(smallWidth);
        } else if (i == R.id.rBtn_paint_normal) {

            mPaintSettingListener.selectLineWidth(normalWidth);
        } else if (i == R.id.rBtn_paint_large) {

            mPaintSettingListener.selectLineWidth(largeWidth);
        }
    }

    public interface PaintSettingListener {
        void selectLineWidth(float width);

        void selectColor(int color);
    }

}
